package com.minisoft.utils;

import cn.hutool.core.lang.Console;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;

import java.sql.SQLException;
import java.util.List;

/**
 * @author JueSu
 * @description: 删除到访客户数据
 * @date 2022/6/10 8:58
 */
public class ClearData {

    public static String DB_CENTER = "center-pro";

    static String poolHost = "http://customer-pool.paas.huafagroup.com";

    /**
     * 待删除手机号码，逗号隔开
     */
    static String mobiles = "13909792555";

    public static void main1(String[] args) throws SQLException {
        //调接口清除潜客数据
        String url = poolHost + "/sys/customer/delTestData";
        JSONObject params = new JSONObject();
        params.set("mobiles",mobiles);
        String result = HttpUtil.post(url, params);
        Console.log("clean pool data result = " + result);

        //删除报备记录customer_report,customer_clue
        String[] mobileArray = mobiles.split(",");
        for (String mobile : mobileArray) {
            cleanCenterData(mobile);
        }
    }

    /**
     * 删除案场分配相关的数据
     * @param args
     */
    public static void main(String[] args) throws Exception {
        String mobile = mobiles;
        Entity clueAllocation = Db.use(DB_CENTER).queryOne("select * from customer_clue_allocation a where a.mobile = ?",mobiles);
        Long id = clueAllocation.getLong("id");
        Long customer_potential_channel_id = clueAllocation.getLong("customer_potential_channel_id");
        Long details_id = clueAllocation.getLong("details_id");
        String clue_id = clueAllocation.getStr("clue_id");

        int i = Db.use(DB_CENTER).execute("delete from customer_clue_allocation where id = ?", id);
        Console.log("delete for {} and result = {} 【customer_clue_allocation】", mobile,i);

        i = Db.use(DB_CENTER).execute("delete from customer_potential_channel where id = ?", customer_potential_channel_id);
        Console.log("delete for {} and result = {} 【customer_potential_channel】", mobile,i);

        i = Db.use(DB_CENTER).execute("delete from cp_allocation_details where id = ?", details_id);
        Console.log("delete for {} and result = {} 【cp_allocation_details】", mobile,i);

        i = Db.use(DB_CENTER).execute("delete from customer_report where clue_id = ?", clue_id);
        Console.log("delete for {} and result = {} 【customer_report】", mobile,i);

        i = Db.use(DB_CENTER).execute("delete from customer_clue where clue_id = ?", clue_id);
        Console.log("delete for {} and result = {} 【customer_clue】", mobile,i);

    }

    private static void cleanCenterData(String mobile) throws SQLException {
        int i = Db.use(DB_CENTER).execute("delete from customer_report where cus_tel = ?", mobile);
        Console.log("delete for {} and result = {} 【customer_report】", mobile,i);

        i = Db.use(DB_CENTER).execute("delete from customer_clue where mobile = ?", mobile);
        Console.log("delete for {} and result = {} 【customer_clue】", mobile,i);

        List<Entity> centerResult = Db.use(DB_CENTER).query("SELECT m.mobile ,o.customer_opportunity_id ,o.customer_id " +
                "from customer_opportunity o JOIN customer_mobile m on o.customer_opportunity_id=m.customer_opportunity_id" +
                " where m.mobile = ?",mobile);
        for (Entity entity : centerResult) {
            String customer_opportunity_id = entity.getStr("customer_opportunity_id");
            String customer_id = entity.getStr("customer_id");
            i = Db.use(DB_CENTER).execute("delete from customer_opportunity where  customer_opportunity_id = ?", customer_opportunity_id);
            Console.log("delete for {} and result = {} 【customer_opportunity】", mobile,i);

            i = Db.use(DB_CENTER).execute("delete from customer_mobile where customer_opportunity_id = ?", customer_opportunity_id);
            Console.log("delete for {} and result = {} 【customer_mobile】", mobile,i);

            i = Db.use(DB_CENTER).execute("delete from customer where customer_id = ?", customer_id);
            Console.log("delete for {} and result = {} 【customer】", mobile,i);

            i = Db.use(DB_CENTER).execute("delete from customer_follow where  customer_opportunity_id = ?", customer_opportunity_id);
            Console.log("delete for {} and result = {} 【customer_follow】", mobile,i);

            i = Db.use(DB_CENTER).execute("delete from visit_log where  customer_opportunity_id = ?", customer_opportunity_id);
            Console.log("delete for {} and result = {} 【visit_log】", mobile,i);

            i = Db.use(DB_CENTER).execute("delete from customer_node_log where  opportunity_id = ?", customer_opportunity_id);
            Console.log("delete for {} and result = {} 【customer_node_log】", mobile,i);

            i = Db.use(DB_CENTER).execute("delete from customer_assin where  customer_opportunity_id = ?", customer_opportunity_id);
            Console.log("delete for {} and result = {} 【customer_assin】", mobile,i);

        }
        //删除通盘客户
        i = Db.use(DB_CENTER).execute("delete from customer_transfer where  out_cus_tel = ?", mobile);
        Console.log("delete for {} and result = {} 【customer_transfer】", mobile,i);
    }
}
